package com.xyht.sca_s.student_manage_system.modules.assetStatistics.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.TimeUtil;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.SmsAssetCategoryManagement;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.SmsAssetFixedStorage;
import com.xyht.sca_s.student_manage_system.modules.asset.mapper.SmsAssetCategoryManagementMapper;
import com.xyht.sca_s.student_manage_system.modules.asset.mapper.SmsAssetFixedStorageMapper;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.entity.SmsAssetConsumableStorage;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.mapper.SmsAssetConsumableStorageMapper;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.entity.SmsAssetConsumableBorrowApply;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.entity.SmsAssetConsumableBorrowApplyGoods;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.mapper.SmsAssetConsumableBorrowApplyGoodsMapper;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableBorrow.mapper.SmsAssetConsumableBorrowApplyMapper;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableOut.entity.SmsAssetConsumableOutStorageApply;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableOut.entity.SmsAssetConsumableOutStorageApplyGoods;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableOut.mapper.SmsAssetConsumableOutStorageApplyGoodsMapper;
import com.xyht.sca_s.student_manage_system.modules.assetConsumableOut.mapper.SmsAssetConsumableOutStorageApplyMapper;
import com.xyht.sca_s.student_manage_system.modules.assetFixedDeal.entity.SmsAssetFixedDealApply;
import com.xyht.sca_s.student_manage_system.modules.assetFixedDeal.entity.SmsAssetFixedDealGoods;
import com.xyht.sca_s.student_manage_system.modules.assetFixedDeal.mapper.SmsAssetFixedDealApplyMapper;
import com.xyht.sca_s.student_manage_system.modules.assetFixedDeal.mapper.SmsAssetFixedDealGoodsMapper;
import com.xyht.sca_s.student_manage_system.modules.assetFixedRepair.entity.SmsAssetFixedRepairApply;
import com.xyht.sca_s.student_manage_system.modules.assetFixedRepair.entity.SmsAssetFixedRepairApplyGoodsDetail;
import com.xyht.sca_s.student_manage_system.modules.assetFixedRepair.mapper.SmsAssetFixedRepairApplyGoodsDetailMapper;
import com.xyht.sca_s.student_manage_system.modules.assetFixedRepair.mapper.SmsAssetFixedRepairApplyMapper;
import com.xyht.sca_s.student_manage_system.modules.assetStatistics.entity.SmsAssetStatistics;
import com.xyht.sca_s.student_manage_system.modules.assetStatistics.entity.resp.*;
import com.xyht.sca_s.student_manage_system.modules.assetStatistics.mapper.SmsAssetStatisticsMapper;
import com.xyht.sca_s.student_manage_system.modules.assetStatistics.service.SmsAssetStatisticsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.modules.org.entity.SmsOrgStructure;
import com.xyht.sca_s.student_manage_system.modules.org.mapper.SmsOrgStructureMapper;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.asset.constant.AssetAllowStatusConstant.ASSET_ALLOW_STATUS_PASS;
import static com.xyht.sca_s.student_manage_system.modules.asset.constant.AssetStatusConstant.ASSET_STATUS_OUT;
import static com.xyht.sca_s.student_manage_system.modules.assetStatistics.constant.DateType.DATE_TYPE_MONTH;
import static com.xyht.sca_s.student_manage_system.modules.assetStatistics.constant.DateType.DATE_TYPE_YEAR;
import static com.xyht.sca_s.student_manage_system.modules.assetStatistics.constant.StatisticsType.*;
import static com.xyht.sca_s.student_manage_system.modules.assetStatistics.constant.ViewType.VIEW_TYPE_NUM;
import static com.xyht.sca_s.student_manage_system.modules.assetStatistics.constant.ViewType.VIEW_TYPE_PRICE;

/**
 * <p>
 * 资产统计表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-10-27
 */
@Service
public class SmsAssetStatisticsServiceImpl extends ServiceImpl<SmsAssetStatisticsMapper, SmsAssetStatistics> implements SmsAssetStatisticsService {

    @Resource
    private SmsAssetFixedStorageMapper smsAssetFixedStorageMapper;
    @Resource
    private SmsUserMapper smsUserMapper;
    @Resource
    private SmsOrgStructureMapper smsOrgStructureMapper;
    @Resource
    private SmsAssetCategoryManagementMapper smsAssetCategoryManagementMapper;
    @Resource
    private SmsAssetFixedRepairApplyMapper smsAssetFixedRepairApplyMapper;
    @Resource
    private SmsAssetFixedRepairApplyGoodsDetailMapper smsAssetFixedRepairApplyGoodsDetailMapper;
    @Resource
    private SmsAssetFixedDealApplyMapper smsAssetFixedDealApplyMapper;
    @Resource
    private SmsAssetFixedDealGoodsMapper smsAssetFixedDealGoodsMapper;
    @Resource
    private SmsAssetConsumableBorrowApplyMapper smsAssetConsumableBorrowApplyMapper;
    @Resource
    private SmsAssetConsumableBorrowApplyGoodsMapper smsAssetConsumableBorrowApplyGoodsMapper;
    @Resource
    private SmsAssetConsumableStorageMapper smsAssetConsumableStorageMapper;
    @Resource
    private SmsAssetConsumableOutStorageApplyMapper smsAssetConsumableOutStorageApplyMapper;
    @Resource
    private SmsAssetConsumableOutStorageApplyGoodsMapper smsAssetConsumableOutStorageApplyGoodsMapper;


    @Override
    public ResponseResult getAssetFixedCategoryStatistics(Integer type, String dateStr, String assetName, String assetModel, String assetBrand, String assetCategoryId,
                                                          Integer pageSize, Integer pageNum) {
        if (isNullOrEmpty(type,dateStr)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        List<SmsAssetFixedCategoryStatisticsResp> list;
        Page<SmsAssetFixedStorage> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsAssetFixedStorage> assetFixedStorageLambdaQueryWrapper = new QueryWrapper<SmsAssetFixedStorage>()
                .select("asset_name as assetName,asset_category as assetCategory,asset_brand as assetBrand,asset_model as assetModel,asset_price as assetPrice,asset_unit as assetUnit," +
                        "count(*) as assetNum,sum(asset_total_depreciation_price) as assetTotalDepreciationPrice,sum(asset_real_price) as assetRealPrice")
                .lambda()
                .groupBy(SmsAssetFixedStorage::getAssetName)
                .groupBy(SmsAssetFixedStorage::getAssetCategory)
                .groupBy(SmsAssetFixedStorage::getAssetBrand)
                .groupBy(SmsAssetFixedStorage::getAssetModel)
                .groupBy(SmsAssetFixedStorage::getAssetPrice)
                .groupBy(SmsAssetFixedStorage::getAssetUnit)
                .orderByDesc(SmsAssetFixedStorage::getCreateTime);

        if (!isNullOrEmpty(assetName)) {
            assetFixedStorageLambdaQueryWrapper.like(SmsAssetFixedStorage::getAssetName, assetName);
        }
        if (!isNullOrEmpty(assetModel)) {
            assetFixedStorageLambdaQueryWrapper.like(SmsAssetFixedStorage::getAssetModel, assetModel);
        }
        if (!isNullOrEmpty(assetBrand)) {
            assetFixedStorageLambdaQueryWrapper.like(SmsAssetFixedStorage::getAssetBrand, assetBrand);
        }
        if (!isNullOrEmpty(assetCategoryId)) {
            assetFixedStorageLambdaQueryWrapper.eq(SmsAssetFixedStorage::getAssetCategory, assetCategoryId);
        }

        smsAssetFixedStorageMapper.selectPage(page, assetFixedStorageLambdaQueryWrapper);
        if (type == DATE_TYPE_MONTH) {//按月统计
            list = getAssetFixedCategoryStatistics(DATE_TYPE_MONTH, dateStr, page);
        } else if (type == DATE_TYPE_YEAR) {//按年统计
            list = getAssetFixedCategoryStatistics(DATE_TYPE_YEAR, dateStr, page);
        }else {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        return CommonResult.success(list,(int)page.getTotal());
    }

    @Override
    public ResponseResult getAssetFixedUseStatistics(String assetName, String assetModel, String assetBrand, String assetCategoryId,
                                                     Integer assetStatus, String ownerName, String departmentName, Integer pageSize, Integer pageNum) {
        Page<SmsAssetFixedStorage> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsAssetFixedStorage> assetFixedStorageLambdaQueryWrapper = new LambdaQueryWrapper<SmsAssetFixedStorage>()
                .ne(SmsAssetFixedStorage::getAssetStatus, ASSET_STATUS_OUT)
                .orderByDesc(SmsAssetFixedStorage::getCreateTime);
        if (!isNullOrEmpty(assetName)) {
            assetFixedStorageLambdaQueryWrapper.like(SmsAssetFixedStorage::getAssetName, assetName);
        }

        if (!isNullOrEmpty(assetModel)) {
            assetFixedStorageLambdaQueryWrapper.like(SmsAssetFixedStorage::getAssetModel,assetModel);
        }

        if (!isNullOrEmpty(assetBrand)) {
            assetFixedStorageLambdaQueryWrapper.like(SmsAssetFixedStorage::getAssetBrand,assetBrand);
        }

        if (!isNullOrEmpty(assetCategoryId)) {
            assetFixedStorageLambdaQueryWrapper.eq(SmsAssetFixedStorage::getAssetCategory,assetCategoryId);
        }

        if (!isNullOrEmpty(assetStatus)) {
            assetFixedStorageLambdaQueryWrapper.eq(SmsAssetFixedStorage::getAssetStatus,assetStatus);
        }

        if (!isNullOrEmpty(ownerName)) {
            List<String> userIdList = smsUserMapper.selectList(new LambdaQueryWrapper<SmsUser>().like(SmsUser::getRealName, ownerName))
                    .stream()
                    .map(SmsUser::getId)
                    .collect(Collectors.toList());
            if (userIdList.isEmpty()) {
                assetFixedStorageLambdaQueryWrapper.eq(SmsAssetFixedStorage::getAssetOwner, "");
            }else {
                assetFixedStorageLambdaQueryWrapper.in(SmsAssetFixedStorage::getAssetOwner, userIdList);
            }
        }

        if (!isNullOrEmpty(departmentName)) {
            List<Integer> orgIdList = smsOrgStructureMapper.selectList(new LambdaQueryWrapper<SmsOrgStructure>().like(SmsOrgStructure::getOrgName, ownerName))
                    .stream()
                    .map(SmsOrgStructure::getId)
                    .collect(Collectors.toList());
            if (orgIdList.isEmpty()) {
                assetFixedStorageLambdaQueryWrapper.eq(SmsAssetFixedStorage::getAssetDepartment, "");
            }else {
                assetFixedStorageLambdaQueryWrapper.in(SmsAssetFixedStorage::getAssetDepartment, orgIdList);
            }
        }
        smsAssetFixedStorageMapper.selectPage(page, assetFixedStorageLambdaQueryWrapper);
        List<SmsAssetFixedUseStatisticsResp> list = page.getRecords()
                .stream()
                .map(smsAssetFixedStorage -> {
                    SmsAssetFixedUseStatisticsResp resp = new SmsAssetFixedUseStatisticsResp();
                    BeanUtils.copyProperties(smsAssetFixedStorage, resp);
                    resp.setAssetTotalOriginalPrice(smsAssetFixedStorage.getAssetPrice());
                    //资产累计折旧价值
                    if (isNullOrEmpty(smsAssetFixedStorage.getAssetTotalDepreciationPrice())) {
                        resp.setAssetTotalDepreciationPrice(new BigDecimal("0.00"));
                    }
                    //净账面价值
                    if (isNullOrEmpty(smsAssetFixedStorage.getAssetRealPrice())) {
                        resp.setAssetRealPrice(new BigDecimal("0.00"));
                    }
                    //分类信息
                    if (!isNullOrEmpty(smsAssetFixedStorage.getAssetCategory())) {
                        SmsAssetCategoryManagement smsAssetCategoryManagement = smsAssetCategoryManagementMapper.selectById(smsAssetFixedStorage.getAssetCategory());
                        if (!isNullOrEmpty(smsAssetCategoryManagement)) {
                            resp.setAssetCategoryName(smsAssetCategoryManagement.getCategoryName());
                        }
                    }
                    //拥有人信息
                    if (!isNullOrEmpty(smsAssetFixedStorage.getAssetOwner())) {
                        SmsUser user = smsUserMapper.selectById(smsAssetFixedStorage.getAssetOwner());
                        if (!isNullOrEmpty(user)) {
                            resp.setAssetOwnerName(user.getRealName());
                        }
                    }
                    //资产所属部门
                    if (!isNullOrEmpty(smsAssetFixedStorage.getAssetDepartment())) {
                        SmsOrgStructure smsOrgStructure = smsOrgStructureMapper.selectById(smsAssetFixedStorage.getAssetDepartment());
                        if (!isNullOrEmpty(smsOrgStructure)) {
                            resp.setAssetDepartmentName(smsOrgStructure.getOrgName());
                        }
                    }
                    return resp;
                })
                .collect(Collectors.toList());

        return CommonResult.success(list, (int) page.getTotal());
    }

    @Override
    public ResponseResult getAssetFixedOutStatistics(String assetName, String assetModel, String assetBrand,
                                                     String assetCategoryId, Integer assetDetailStatus, String ownerName, String departmentName,
                                                     Integer pageSize, Integer pageNum) {
        Page<SmsAssetFixedStorage> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsAssetFixedStorage> assetFixedStorageLambdaQueryWrapper = new LambdaQueryWrapper<SmsAssetFixedStorage>()
                .eq(SmsAssetFixedStorage::getAssetStatus, ASSET_STATUS_OUT)
                .orderByDesc(SmsAssetFixedStorage::getCreateTime);
        if (!isNullOrEmpty(assetName)) {
            assetFixedStorageLambdaQueryWrapper.like(SmsAssetFixedStorage::getAssetName, assetName);
        }

        if (!isNullOrEmpty(assetModel)) {
            assetFixedStorageLambdaQueryWrapper.like(SmsAssetFixedStorage::getAssetModel,assetModel);
        }

        if (!isNullOrEmpty(assetBrand)) {
            assetFixedStorageLambdaQueryWrapper.like(SmsAssetFixedStorage::getAssetBrand,assetBrand);
        }

        if (!isNullOrEmpty(assetCategoryId)) {
            assetFixedStorageLambdaQueryWrapper.eq(SmsAssetFixedStorage::getAssetCategory,assetCategoryId);
        }

        if (!isNullOrEmpty(assetDetailStatus)) {
            assetFixedStorageLambdaQueryWrapper.eq(SmsAssetFixedStorage::getAssetDetailStatus,assetDetailStatus);
        }

        if (!isNullOrEmpty(ownerName)) {
            List<String> userIdList = smsUserMapper.selectList(new LambdaQueryWrapper<SmsUser>().like(SmsUser::getRealName, ownerName))
                    .stream()
                    .map(SmsUser::getId)
                    .collect(Collectors.toList());
            if (userIdList.isEmpty()) {
                assetFixedStorageLambdaQueryWrapper.eq(SmsAssetFixedStorage::getAssetOwner, "");
            }else {
                assetFixedStorageLambdaQueryWrapper.in(SmsAssetFixedStorage::getAssetOwner, userIdList);
            }
        }

        if (!isNullOrEmpty(departmentName)) {
            List<Integer> orgIdList = smsOrgStructureMapper.selectList(new LambdaQueryWrapper<SmsOrgStructure>().like(SmsOrgStructure::getOrgName, ownerName))
                    .stream()
                    .map(SmsOrgStructure::getId)
                    .collect(Collectors.toList());
            if (orgIdList.isEmpty()) {
                assetFixedStorageLambdaQueryWrapper.eq(SmsAssetFixedStorage::getAssetDepartment, "");
            }else {
                assetFixedStorageLambdaQueryWrapper.in(SmsAssetFixedStorage::getAssetDepartment, orgIdList);
            }
        }
        smsAssetFixedStorageMapper.selectPage(page, assetFixedStorageLambdaQueryWrapper);
        List<SmsAssetFixedOutStatisticsResp> list = page.getRecords()
                .stream()
                .map(smsAssetFixedStorage -> {
                    SmsAssetFixedOutStatisticsResp resp = new SmsAssetFixedOutStatisticsResp();
                    BeanUtils.copyProperties(smsAssetFixedStorage, resp);
                    resp.setAssetTotalOriginalPrice(smsAssetFixedStorage.getAssetPrice());
                    //分类信息
                    if (!isNullOrEmpty(smsAssetFixedStorage.getAssetCategory())) {
                        SmsAssetCategoryManagement smsAssetCategoryManagement = smsAssetCategoryManagementMapper.selectById(smsAssetFixedStorage.getAssetCategory());
                        if (!isNullOrEmpty(smsAssetCategoryManagement)) {
                            resp.setAssetCategoryName(smsAssetCategoryManagement.getCategoryName());
                        }
                    }
                    //拥有人信息
                    if (!isNullOrEmpty(smsAssetFixedStorage.getAssetOwner())) {
                        SmsUser user = smsUserMapper.selectById(smsAssetFixedStorage.getAssetOwner());
                        if (!isNullOrEmpty(user)) {
                            resp.setAssetOwnerName(user.getRealName());
                        }
                    }
                    //资产所属部门
                    if (!isNullOrEmpty(smsAssetFixedStorage.getAssetDepartment())) {
                        SmsOrgStructure smsOrgStructure = smsOrgStructureMapper.selectById(smsAssetFixedStorage.getAssetDepartment());
                        if (!isNullOrEmpty(smsOrgStructure)) {
                            resp.setAssetDepartmentName(smsOrgStructure.getOrgName());
                        }
                    }
                    return resp;
                })
                .collect(Collectors.toList());

        return CommonResult.success(list, (int) page.getTotal());
    }

    @Override
    public ResponseResult getAssetFixedNewAddAndRepairAndOutStatistics(String yearStr, Integer type) {
        if (isNullOrEmpty(yearStr, type)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        List<SmsAssetFixedNewAddStatisticsResp> list ;
        if (type == STATISTICS_TYPE_ADD) {//新增
            list = getAssetFixedNewAddAndRepairAndOutStatistics(STATISTICS_TYPE_ADD, yearStr);
        }else if (type == STATISTICS_TYPE_REPAIR) {//维修
            list = getAssetFixedNewAddAndRepairAndOutStatistics(STATISTICS_TYPE_REPAIR, yearStr);
        }else if (type == STATISTICS_TYPE_OUT) {//处置
            list = getAssetFixedNewAddAndRepairAndOutStatistics(STATISTICS_TYPE_OUT, yearStr);
        }else {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        return CommonResult.success(list);
    }

    @Override
    public ResponseResult getAssetConsumableInStorageAndBorrowStatistics(String yearStr, Integer type) {
        if (isNullOrEmpty(yearStr, type)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        List<SmsAssetConsumableStatisticsResp> list;
        if (type == STATISTICS_TYPE_CONSUMABLE_IN) {//入库
            list = getAssetConsumableStatistics(STATISTICS_TYPE_CONSUMABLE_IN, yearStr);
        }else if (type == STATISTICS_TYPE_CONSUMABLE_BORROW) {//领用
            list = getAssetConsumableStatistics(STATISTICS_TYPE_CONSUMABLE_BORROW, yearStr);
        }else {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        return CommonResult.success(list);
    }

    @Override
    public ResponseResult getAssetFixedOverViewStatistics(Integer viewType) {
        if (isNullOrEmpty(viewType)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsAssetFixedOverViewStatisticsListResp resp = new SmsAssetFixedOverViewStatisticsListResp();
        List<SmsAssetFixedOverViewStatisticsResp> assetInfoList = new ArrayList<>();
        //总数
        Integer count = smsAssetFixedStorageMapper.selectCount(null);
        resp.setAssetTotalNum(count);
        if (count < 1) {
            return CommonResult.success(resp);
        }

        int index = 0;
        int ration = 0;
        switch (viewType) {
            case VIEW_TYPE_NUM:
                List<SmsAssetFixedStorage> fixedStorageNumList = smsAssetFixedStorageMapper.selectList(new QueryWrapper<SmsAssetFixedStorage>()
                        .select("asset_status as assetStatus,count(*) as assetNum")
                        .lambda()
                        .groupBy(SmsAssetFixedStorage::getAssetStatus));
                if (fixedStorageNumList.isEmpty()) {
                    return CommonResult.success(resp);
                }

                for (SmsAssetFixedStorage fixedStorage : fixedStorageNumList) {
                    SmsAssetFixedOverViewStatisticsResp statisticsResp = new SmsAssetFixedOverViewStatisticsResp();
                    statisticsResp.setAssetStatus(fixedStorage.getAssetStatus());
                    statisticsResp.setAsseTotalNum(fixedStorage.getAssetNum());
                    //占比
                    if (index == fixedStorageNumList.size() - 1) {
                        statisticsResp.setAssetRatio(100 - ration);
                    }else {
                        double assetRation = (double) fixedStorage.getAssetNum() / count * 100;
                        statisticsResp.setAssetRatio((int) assetRation);
                        ration += (int) assetRation;
                    }
                    assetInfoList.add(statisticsResp);
                    index ++;
                }
                break;
            case VIEW_TYPE_PRICE:
                //总金额
                SmsAssetFixedStorage fixedStorageTotalPrice = smsAssetFixedStorageMapper.selectOne(new QueryWrapper<SmsAssetFixedStorage>()
                        .select("sum(asset_price) as assetPrice"));
                if (isNullOrEmpty(fixedStorageTotalPrice)) {
                    return CommonResult.failed(CommonCodeEnum.FAIL);
                }
                BigDecimal assetTotalPrice = fixedStorageTotalPrice.getAssetPrice();
                resp.setAssetTotalPrice(assetTotalPrice);

                List<SmsAssetFixedStorage> fixedStorageList = smsAssetFixedStorageMapper.selectList(new QueryWrapper<SmsAssetFixedStorage>()
                        .select("asset_status as assetStatus,sum(asset_price) as assetPrice")
                        .lambda()
                        .groupBy(SmsAssetFixedStorage::getAssetStatus));
                if (fixedStorageList.isEmpty()) {
                    return CommonResult.success(resp);
                }

                for (SmsAssetFixedStorage fixedStorage : fixedStorageList) {
                    SmsAssetFixedOverViewStatisticsResp statisticsResp = new SmsAssetFixedOverViewStatisticsResp();
                    statisticsResp.setAssetStatus(fixedStorage.getAssetStatus());
                    statisticsResp.setAssetTotalPrice(fixedStorage.getAssetPrice());
                    //占比
                    if (index == fixedStorageList.size() - 1) {
                        statisticsResp.setAssetRatio(100 - ration);
                    }else {
                        BigDecimal assetRation = fixedStorage.getAssetPrice().divide(assetTotalPrice, 2, RoundingMode.HALF_UP);
                        ration = assetRation.intValue();
                        statisticsResp.setAssetRatio(assetRation.intValue());
                        ration += ration;
                    }
                    assetInfoList.add(statisticsResp);
                    index ++;
                }
                break;
            default:
                return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        resp.setAssetInfoList(assetInfoList);
        return CommonResult.success(resp);
    }

    @Override
    public ResponseResult getAssetFixedCategoryAndDepAndPlaceStatistics(Integer statisticsType) {
        if (isNullOrEmpty(statisticsType)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        List<SmsAssetFixedCategoryAndDepAndPlaceStatisticsResp> list;

        switch (statisticsType) {
            case STATISTICS_TYPE_CATEGORY:
                list = smsAssetFixedStorageMapper.selectList(new QueryWrapper<SmsAssetFixedStorage>()
                                .select("count(*) as assetNum,sum(asset_price) as assetPrice,asset_category as assetCategory")
                        .lambda()
                        .groupBy(SmsAssetFixedStorage::getAssetCategory))
                        .stream()
                        .map(smsAssetFixedStorage -> {
                            SmsAssetFixedCategoryAndDepAndPlaceStatisticsResp statisticsResp = new SmsAssetFixedCategoryAndDepAndPlaceStatisticsResp();
                            SmsAssetCategoryManagement smsAssetCategoryManagement = smsAssetCategoryManagementMapper.selectById(smsAssetFixedStorage.getAssetCategory());
                            if (!isNullOrEmpty(smsAssetCategoryManagement)) {
                                statisticsResp.setName(smsAssetCategoryManagement.getCategoryName());
                            }
                            statisticsResp.setAsseTotalNum(smsAssetFixedStorage.getAssetNum());
                            statisticsResp.setAssetTotalPrice(smsAssetFixedStorage.getAssetPrice());
                            return statisticsResp;
                        })
                        .collect(Collectors.toList());
                break;
            case STATISTICS_TYPE_PLACE:
                list = smsAssetFixedStorageMapper.selectList(new QueryWrapper<SmsAssetFixedStorage>()
                                .select("count(*) as assetNum,sum(asset_price) as assetPrice,asset_place as assetPlace")
                                .lambda()
                                .isNotNull(SmsAssetFixedStorage::getAssetPlace)
                                .groupBy(SmsAssetFixedStorage::getAssetPlace))
                        .stream()
                        .map(smsAssetFixedStorage -> {
                            SmsAssetFixedCategoryAndDepAndPlaceStatisticsResp statisticsResp = new SmsAssetFixedCategoryAndDepAndPlaceStatisticsResp();
                            statisticsResp.setName(smsAssetFixedStorage.getAssetPlace());
                            statisticsResp.setAsseTotalNum(smsAssetFixedStorage.getAssetNum());
                            statisticsResp.setAssetTotalPrice(smsAssetFixedStorage.getAssetPrice());
                            return statisticsResp;
                        })
                        .collect(Collectors.toList());
                break;
            case STATISTICS_TYPE_DEP:
                list = smsAssetFixedStorageMapper.selectList(new QueryWrapper<SmsAssetFixedStorage>()
                                .select("count(*) as assetNum,sum(asset_price) as assetPrice,asset_department as assetDepartment")
                                .lambda()
                                .isNotNull(SmsAssetFixedStorage::getAssetDepartment)
                                .groupBy(SmsAssetFixedStorage::getAssetDepartment))
                        .stream()
                        .map(smsAssetFixedStorage -> {
                            SmsAssetFixedCategoryAndDepAndPlaceStatisticsResp statisticsResp = new SmsAssetFixedCategoryAndDepAndPlaceStatisticsResp();
                            SmsOrgStructure smsOrgStructure = smsOrgStructureMapper.selectById(smsAssetFixedStorage.getAssetDepartment());
                            if (!isNullOrEmpty(smsOrgStructure)) {
                                statisticsResp.setName(smsOrgStructure.getOrgName());
                            }
                            statisticsResp.setAsseTotalNum(smsAssetFixedStorage.getAssetNum());
                            statisticsResp.setAssetTotalPrice(smsAssetFixedStorage.getAssetPrice());
                            return statisticsResp;
                        })
                        .collect(Collectors.toList());
                break;
                default:
                    return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        return CommonResult.success(list);
    }

    @Override
    public ResponseResult getAssetFixedNewAddAndRepairAndOutYearStatistics(String yearStr) {
        if (isNullOrEmpty(yearStr) || yearStr.length() != 7) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        // 设置年份
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Integer.parseInt(yearStr.substring(0,4)));
        //设置月份
        calendar.set(Calendar.MONTH, Integer.parseInt(yearStr.substring(5,7))-1);
        calendar.set(Calendar.DATE, 1);
        Date thisYearFistDate = calendar.getTime();
        Date startDateOfMonth = TimeUtil.GetStartDateOfMonth(thisYearFistDate);//该月起始时间
        Date endDateOfMonth = TimeUtil.GetEndDateOfMonth(thisYearFistDate);//该月最后一天

        SmsAssetFixedNewAndRepairAndOutYearStatisticsResp resp = new SmsAssetFixedNewAndRepairAndOutYearStatisticsResp();
        int assetRepairTotalNum = 0;
        BigDecimal assetRepairTotalPrice = BigDecimal.ZERO;
        int assetDealTotalNum = 0;
        BigDecimal assetDealTotalPrice = BigDecimal.ZERO;
        //新增
        SmsAssetFixedStorage smsAssetFixedStorage = smsAssetFixedStorageMapper.selectOne(new QueryWrapper<SmsAssetFixedStorage>()
                .select("count(*) as assetNum,sum(asset_price) as assetPrice")
                .lambda()
                .ge(SmsAssetFixedStorage::getCreateTime, startDateOfMonth)
                .le(SmsAssetFixedStorage::getCreateTime, endDateOfMonth));
        if (!isNullOrEmpty(smsAssetFixedStorage)) {
            resp.setAssetNewAddTotalNum(smsAssetFixedStorage.getAssetNum());
            resp.setAssetNewAddTotalPrice(isNullOrEmpty(smsAssetFixedStorage.getAssetPrice()) ? BigDecimal.ZERO : smsAssetFixedStorage.getAssetPrice());
        }

        //维修
        List<SmsAssetFixedRepairApply> assetFixedRepairApplyList = smsAssetFixedRepairApplyMapper.selectList(new LambdaQueryWrapper<SmsAssetFixedRepairApply>()
                .eq(SmsAssetFixedRepairApply::getAllowStatus, ASSET_ALLOW_STATUS_PASS)
                .ge(SmsAssetFixedRepairApply::getCreateTime, startDateOfMonth)
                .le(SmsAssetFixedRepairApply::getCreateTime, endDateOfMonth));
        if (!assetFixedRepairApplyList.isEmpty()) {
            for (SmsAssetFixedRepairApply assetFixedRepairApply : assetFixedRepairApplyList) {
                List<SmsAssetFixedRepairApplyGoodsDetail> assetFixedRepairApplyGoodsDetailList = smsAssetFixedRepairApplyGoodsDetailMapper.selectList(new LambdaQueryWrapper<SmsAssetFixedRepairApplyGoodsDetail>()
                        .eq(SmsAssetFixedRepairApplyGoodsDetail::getApplyId, assetFixedRepairApply.getId()));
                if (!assetFixedRepairApplyGoodsDetailList.isEmpty()) {
                    for (SmsAssetFixedRepairApplyGoodsDetail smsAssetFixedRepairApplyGoodsDetail : assetFixedRepairApplyGoodsDetailList) {
                        SmsAssetFixedStorage smsAssetFixedStorage1 = smsAssetFixedStorageMapper.selectById(smsAssetFixedRepairApplyGoodsDetail.getAssetId());
                        assetRepairTotalNum += 1;
                        assetRepairTotalPrice = assetRepairTotalPrice.add(smsAssetFixedStorage1.getAssetPrice());
                    }
                }
            }

        }

        //处置
        LambdaQueryWrapper<SmsAssetFixedDealApply> assetFixedDealApplyLambdaQueryWrapper = new LambdaQueryWrapper<SmsAssetFixedDealApply>()
                .ne(SmsAssetFixedDealApply::getAllowStatus, ASSET_ALLOW_STATUS_PASS)
                .ge(SmsAssetFixedDealApply::getCreateTime, startDateOfMonth)
                .le(SmsAssetFixedDealApply::getCreateTime, endDateOfMonth);

        assetDealTotalNum = smsAssetFixedDealApplyMapper.selectCount(assetFixedDealApplyLambdaQueryWrapper);


        if (assetDealTotalNum > 0) {
            List<SmsAssetFixedDealApply> assetFixedDealApplyList = smsAssetFixedDealApplyMapper.selectList(assetFixedDealApplyLambdaQueryWrapper);
            if (!assetFixedRepairApplyList.isEmpty()) {//所有的金额加起来
                for (SmsAssetFixedDealApply assetFixedDealApply : assetFixedDealApplyList) {
                    List<SmsAssetFixedDealGoods> assetFixedDealGoodsList = smsAssetFixedDealGoodsMapper.selectList(new LambdaQueryWrapper<SmsAssetFixedDealGoods>()
                            .eq(SmsAssetFixedDealGoods::getApplyId, assetFixedDealApply.getId()));
                    if (!assetFixedDealGoodsList.isEmpty()) {
                        for (SmsAssetFixedDealGoods smsAssetFixedDealGoods : assetFixedDealGoodsList) {
                            SmsAssetFixedStorage smsAssetFixedStorage2 = smsAssetFixedStorageMapper.selectById(smsAssetFixedDealGoods.getAssetId());
                            if (!isNullOrEmpty(smsAssetFixedStorage2)) {
                                assetRepairTotalPrice = assetRepairTotalPrice.add(smsAssetFixedStorage.getAssetPrice());
                            }
                        }

                    }

                }
            }
        }
        resp.setAssetRepairTotalNum(assetRepairTotalNum);
        resp.setAssetRepairTotalPrice(assetRepairTotalPrice);
        resp.setAssetDealTotalNum(assetDealTotalNum);
        resp.setAssetDealTotalPrice(assetDealTotalPrice);
        return CommonResult.success(resp);
    }

    @Override
    public ResponseResult getAssetConsumableInAndOutStorageStatistics(String yearStr) {
        if (isNullOrEmpty(yearStr)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        // 设置年份
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Integer.parseInt(yearStr.substring(0,4)));
        //设置月份
        calendar.set(Calendar.MONTH, Integer.parseInt(yearStr.substring(5,7))-1);
        calendar.set(Calendar.DATE, 1);
        Date thisYearFistDate = calendar.getTime();
        Date startDateOfMonth = TimeUtil.GetStartDateOfMonth(thisYearFistDate);//该月起始时间
        Date endDateOfMonth = TimeUtil.GetEndDateOfMonth(thisYearFistDate);//该月最后一天

        SmsAssetConsumableInAndOutStorageStatisticsResp resp = new SmsAssetConsumableInAndOutStorageStatisticsResp();
        //入库
        LambdaQueryWrapper<SmsAssetConsumableStorage> assetConsumableStorageLambdaQueryWrapper = new LambdaQueryWrapper<SmsAssetConsumableStorage>()
                .ge(SmsAssetConsumableStorage::getCreateTime, startDateOfMonth)
                .le(SmsAssetConsumableStorage::getCreateTime, endDateOfMonth);

        int assetInStorageTotalNum = smsAssetConsumableStorageMapper.selectCount(assetConsumableStorageLambdaQueryWrapper);
        BigDecimal assetInStorageTotalPrice = BigDecimal.ZERO;

        if (assetInStorageTotalNum > 0) {
            List<SmsAssetConsumableStorage> assetConsumableStorageList = smsAssetConsumableStorageMapper.selectList(assetConsumableStorageLambdaQueryWrapper);
            if (!assetConsumableStorageList.isEmpty()) {//所有的金额加起来
                for (SmsAssetConsumableStorage assetConsumableStorage : assetConsumableStorageList) {
                    assetInStorageTotalPrice = assetInStorageTotalPrice.add(assetConsumableStorage.getConsumablePrice());
                }
            }
        }
        //出库
        LambdaQueryWrapper<SmsAssetConsumableOutStorageApply> assetConsumableOutStorageApplyLambdaQueryWrapper = new LambdaQueryWrapper<SmsAssetConsumableOutStorageApply>()
                .eq(SmsAssetConsumableOutStorageApply::getAllowStatus,ASSET_ALLOW_STATUS_PASS)
                .ge(SmsAssetConsumableOutStorageApply::getCreateTime, startDateOfMonth)
                .le(SmsAssetConsumableOutStorageApply::getCreateTime, endDateOfMonth);
        int assetOutStorageTotalNum = smsAssetConsumableOutStorageApplyMapper.selectCount(assetConsumableOutStorageApplyLambdaQueryWrapper);
        BigDecimal assetOutStorageTotalPrice = BigDecimal.ZERO;
        if (assetOutStorageTotalNum > 0) {
            List<SmsAssetConsumableOutStorageApply> consumableOutStorageApplyList = smsAssetConsumableOutStorageApplyMapper.selectList(assetConsumableOutStorageApplyLambdaQueryWrapper);
            if (!consumableOutStorageApplyList.isEmpty()) {
                for (SmsAssetConsumableOutStorageApply smsAssetConsumableOutStorageApply : consumableOutStorageApplyList) {
                    List<SmsAssetConsumableOutStorageApplyGoods> smsAssetConsumableOutStorageApplyGoods = smsAssetConsumableOutStorageApplyGoodsMapper.selectList(new LambdaQueryWrapper<SmsAssetConsumableOutStorageApplyGoods>()
                            .eq(SmsAssetConsumableOutStorageApplyGoods::getApplyId, smsAssetConsumableOutStorageApply.getId()));
                    if (!smsAssetConsumableOutStorageApplyGoods.isEmpty()) {
                        for (SmsAssetConsumableOutStorageApplyGoods smsAssetConsumableOutStorageApplyGood : smsAssetConsumableOutStorageApplyGoods) {
                            SmsAssetConsumableStorage smsAssetConsumableStorage = smsAssetConsumableStorageMapper.selectById(smsAssetConsumableOutStorageApplyGood.getAssetConsumableId());
                            if (!isNullOrEmpty(smsAssetConsumableStorage)) {
                                assetOutStorageTotalPrice = assetOutStorageTotalPrice.add(smsAssetConsumableStorage.getConsumablePrice());
                            }
                        }
                    }
                }
            }
        }
        resp.setAssetInStorageTotalNum(assetInStorageTotalNum);
        resp.setAssetInStorageTotalPrice(assetInStorageTotalPrice);
        resp.setAssetOutStorageTotalNum(assetOutStorageTotalNum);
        resp.setAssetOutStorageTotalPrice(assetOutStorageTotalPrice);
        return CommonResult.success(resp);
    }

    private List<SmsAssetFixedNewAddStatisticsResp> getAssetFixedNewAddAndRepairAndOutStatistics(Integer type, String yearStr) {
        List<SmsAssetFixedNewAddStatisticsResp> list = new ArrayList<>();
        // 设置年份
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Integer.parseInt(yearStr));
        //设置月份
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DATE, 1);
        Date thisYearFistDate = calendar.getTime();
        for (int i = 1; i < 13; i++) {
            int assetTotalNum = 0;//总数
            BigDecimal assetTotalPrice = BigDecimal.ZERO;//总金额

            Date startDateOfMonth = TimeUtil.GetStartDateOfMonth(thisYearFistDate);//该月起始时间
            Date endDateOfMonth = TimeUtil.GetEndDateOfMonth(thisYearFistDate);//该月最后一天
            //构造条件查询
            if (type == STATISTICS_TYPE_ADD){//新增
                LambdaQueryWrapper<SmsAssetFixedStorage> fixedStorageLambdaQueryWrapper = new LambdaQueryWrapper<SmsAssetFixedStorage>()
                        .ge(SmsAssetFixedStorage::getCreateTime, startDateOfMonth)
                        .le(SmsAssetFixedStorage::getCreateTime, endDateOfMonth);

                assetTotalNum = smsAssetFixedStorageMapper.selectCount(fixedStorageLambdaQueryWrapper);


                if (assetTotalNum > 0) {
                    List<SmsAssetFixedStorage> fixedStorageList = smsAssetFixedStorageMapper.selectList(fixedStorageLambdaQueryWrapper);
                    if (!fixedStorageList.isEmpty()) {//所有的金额加起来
                        for (SmsAssetFixedStorage smsAssetFixedStorage : fixedStorageList) {
                            assetTotalPrice = assetTotalPrice.add(smsAssetFixedStorage.getAssetPrice());
                        }
                    }
                }
            }else if (type == STATISTICS_TYPE_REPAIR){//维修

                LambdaQueryWrapper<SmsAssetFixedRepairApply> assetFixedRepairApplyLambdaQueryWrapper = new LambdaQueryWrapper<SmsAssetFixedRepairApply>()
                        .eq(SmsAssetFixedRepairApply::getAllowStatus, ASSET_ALLOW_STATUS_PASS)//审批通过
                        .ge(SmsAssetFixedRepairApply::getCreateTime, startDateOfMonth)
                        .le(SmsAssetFixedRepairApply::getCreateTime, endDateOfMonth);

                assetTotalNum = smsAssetFixedRepairApplyMapper.selectCount(assetFixedRepairApplyLambdaQueryWrapper);


                if (assetTotalNum > 0) {
                    List<SmsAssetFixedRepairApply> assetFixedRepairApplyList = smsAssetFixedRepairApplyMapper.selectList(assetFixedRepairApplyLambdaQueryWrapper);
                    if (!assetFixedRepairApplyList.isEmpty()) {//所有的单价金额加起来
                        for (SmsAssetFixedRepairApply assetFixedRepairApply : assetFixedRepairApplyList) {
                            List<SmsAssetFixedRepairApplyGoodsDetail> fixedRepairApplyGoodsDetailList = smsAssetFixedRepairApplyGoodsDetailMapper.selectList(new LambdaQueryWrapper<SmsAssetFixedRepairApplyGoodsDetail>()
                                    .eq(SmsAssetFixedRepairApplyGoodsDetail::getApplyId, assetFixedRepairApply.getId()));
                            if (!fixedRepairApplyGoodsDetailList.isEmpty()) {
                                for (SmsAssetFixedRepairApplyGoodsDetail smsAssetFixedRepairApplyGoodsDetail : fixedRepairApplyGoodsDetailList) {
                                    SmsAssetFixedStorage smsAssetFixedStorage = smsAssetFixedStorageMapper.selectById(smsAssetFixedRepairApplyGoodsDetail.getAssetId());
                                    if (!isNullOrEmpty(smsAssetFixedStorage)) {
                                        assetTotalPrice = assetTotalPrice.add(smsAssetFixedStorage.getAssetPrice());
                                    }
                                }

                            }

                        }
                    }
                }

            }else if (type == STATISTICS_TYPE_OUT){//处置
                LambdaQueryWrapper<SmsAssetFixedDealApply> assetFixedDealApplyLambdaQueryWrapper = new LambdaQueryWrapper<SmsAssetFixedDealApply>()
                        .ne(SmsAssetFixedDealApply::getAllowStatus, ASSET_ALLOW_STATUS_PASS)
                        .ge(SmsAssetFixedDealApply::getCreateTime, startDateOfMonth)
                        .le(SmsAssetFixedDealApply::getCreateTime, endDateOfMonth);

                assetTotalNum = smsAssetFixedDealApplyMapper.selectCount(assetFixedDealApplyLambdaQueryWrapper);


                if (assetTotalNum > 0) {
                    List<SmsAssetFixedDealApply> assetFixedRepairApplyList = smsAssetFixedDealApplyMapper.selectList(assetFixedDealApplyLambdaQueryWrapper);
                    if (!assetFixedRepairApplyList.isEmpty()) {//所有的金额加起来
                        for (SmsAssetFixedDealApply assetFixedDealApply : assetFixedRepairApplyList) {
                            List<SmsAssetFixedDealGoods> assetFixedDealGoodsList = smsAssetFixedDealGoodsMapper.selectList(new LambdaQueryWrapper<SmsAssetFixedDealGoods>()
                                    .eq(SmsAssetFixedDealGoods::getApplyId, assetFixedDealApply.getId()));
                            if (!assetFixedDealGoodsList.isEmpty()) {
                                for (SmsAssetFixedDealGoods smsAssetFixedDealGoods : assetFixedDealGoodsList) {
                                    SmsAssetFixedStorage smsAssetFixedStorage = smsAssetFixedStorageMapper.selectById(smsAssetFixedDealGoods.getAssetId());
                                    if (!isNullOrEmpty(smsAssetFixedStorage)) {
                                        assetTotalPrice = assetTotalPrice.add(smsAssetFixedStorage.getAssetPrice());
                                    }
                                }

                            }

                        }
                    }
                }

            }

            thisYearFistDate = TimeUtil.AddMonths(thisYearFistDate, 1);

            SmsAssetFixedNewAddStatisticsResp resp = new SmsAssetFixedNewAddStatisticsResp();
            resp.setAssetTotalNum(assetTotalNum);
            resp.setAssetTotalPrice(assetTotalPrice);
            resp.setMonth(TimeUtil.DateFormatSting(startDateOfMonth).substring(5, 7));
            list.add(resp);
        }

        return list;
    }

    private List<SmsAssetConsumableStatisticsResp> getAssetConsumableStatistics(Integer type,String yearStr) {
        List<SmsAssetConsumableStatisticsResp> list = new ArrayList<>();
        // 设置年份
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Integer.parseInt(yearStr));
        //设置月份
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DATE, 1);
        Date thisYearFistDate = calendar.getTime();
        for (int i = 1; i < 13; i++) {
            int assetTotalNum = 0;//总数
            BigDecimal assetTotalPrice = BigDecimal.ZERO;//总金额

            Date startDateOfMonth = TimeUtil.GetStartDateOfMonth(thisYearFistDate);//该月起始时间
            Date endDateOfMonth = TimeUtil.GetEndDateOfMonth(thisYearFistDate);//该月最后一天
            //构造条件查询
            if (type == STATISTICS_TYPE_CONSUMABLE_IN){//入库
                LambdaQueryWrapper<SmsAssetConsumableStorage> assetConsumableStorageLambdaQueryWrapper = new LambdaQueryWrapper<SmsAssetConsumableStorage>()
                        .ge(SmsAssetConsumableStorage::getCreateTime, startDateOfMonth)
                        .le(SmsAssetConsumableStorage::getCreateTime, endDateOfMonth);

                assetTotalNum = smsAssetConsumableStorageMapper.selectCount(assetConsumableStorageLambdaQueryWrapper);


                if (assetTotalNum > 0) {
                    List<SmsAssetConsumableStorage> assetConsumableStorageList = smsAssetConsumableStorageMapper.selectList(assetConsumableStorageLambdaQueryWrapper);
                    if (!assetConsumableStorageList.isEmpty()) {//所有的金额加起来
                        for (SmsAssetConsumableStorage assetConsumableStorage : assetConsumableStorageList) {
                            assetTotalPrice = assetTotalPrice.add(assetConsumableStorage.getConsumablePrice());
                        }
                    }
                }
            }else if (type == STATISTICS_TYPE_CONSUMABLE_BORROW){//领用

                LambdaQueryWrapper<SmsAssetConsumableBorrowApply> assetConsumableBorrowApplyLambdaQueryWrapper = new LambdaQueryWrapper<SmsAssetConsumableBorrowApply>()
                        .eq(SmsAssetConsumableBorrowApply::getAllowStatus, ASSET_ALLOW_STATUS_PASS)//审批通过
                        .ge(SmsAssetConsumableBorrowApply::getCreateTime, startDateOfMonth)
                        .le(SmsAssetConsumableBorrowApply::getCreateTime, endDateOfMonth);

                assetTotalNum = smsAssetConsumableBorrowApplyMapper.selectCount(assetConsumableBorrowApplyLambdaQueryWrapper);


                if (assetTotalNum > 0) {
                    List<SmsAssetConsumableBorrowApply> assetConsumableBorrowApplyList = smsAssetConsumableBorrowApplyMapper.selectList(assetConsumableBorrowApplyLambdaQueryWrapper);
                    if (!assetConsumableBorrowApplyList.isEmpty()) {//所有的单价金额加起来
                        for (SmsAssetConsumableBorrowApply assetConsumableBorrowApply : assetConsumableBorrowApplyList) {
                            List<SmsAssetConsumableBorrowApplyGoods> assetConsumableBorrowApplyGoodsList = smsAssetConsumableBorrowApplyGoodsMapper.selectList(new LambdaQueryWrapper<SmsAssetConsumableBorrowApplyGoods>()
                                    .eq(SmsAssetConsumableBorrowApplyGoods::getApplyId, assetConsumableBorrowApply.getId()));
                            if (!assetConsumableBorrowApplyGoodsList.isEmpty()) {
                                for (SmsAssetConsumableBorrowApplyGoods assetConsumableBorrowApplyGoods : assetConsumableBorrowApplyGoodsList) {
                                    SmsAssetConsumableStorage smsAssetConsumableStorage = smsAssetConsumableStorageMapper.selectById(assetConsumableBorrowApplyGoods.getAssetConsumableId());
                                    if (!isNullOrEmpty(smsAssetConsumableStorage)) {
                                        assetTotalPrice = assetTotalPrice.add(smsAssetConsumableStorage.getConsumablePrice());
                                    }
                                }

                            }

                        }
                    }
                }

            }

            thisYearFistDate = TimeUtil.AddMonths(thisYearFistDate, 1);

            SmsAssetConsumableStatisticsResp resp = new SmsAssetConsumableStatisticsResp();
            resp.setAssetTotalNum(assetTotalNum);
            resp.setAssetTotalPrice(assetTotalPrice);
            resp.setMonth(TimeUtil.DateFormatSting(startDateOfMonth).substring(5, 7));
            list.add(resp);
        }

        return list;
    }
    private List<SmsAssetFixedCategoryStatisticsResp> getAssetFixedCategoryStatistics(Integer type, String dateStr,Page<SmsAssetFixedStorage> page){

        List<SmsAssetFixedCategoryStatisticsResp> list = page.getRecords()
                .stream()
                .map(assetFixedStorage -> {
                    SmsAssetFixedCategoryStatisticsResp resp = new SmsAssetFixedCategoryStatisticsResp();
                    BeanUtils.copyProperties(assetFixedStorage, resp);
                    //资产累计折旧价值
                    if (isNullOrEmpty(assetFixedStorage.getAssetTotalDepreciationPrice())) {
                        resp.setAssetTotalDepreciationPrice(new BigDecimal("0.00"));
                    }
                    //净账面价值
                    if (isNullOrEmpty(assetFixedStorage.getAssetRealPrice())) {
                        resp.setAssetRealPrice(new BigDecimal("0.00"));
                    }
                    //分类信息
                    if (!isNullOrEmpty(assetFixedStorage.getAssetCategory())) {
                        SmsAssetCategoryManagement smsAssetCategoryManagement = smsAssetCategoryManagementMapper.selectById(assetFixedStorage.getAssetCategory());
                        if (!isNullOrEmpty(smsAssetCategoryManagement)) {
                            resp.setAssetCategoryName(smsAssetCategoryManagement.getCategoryName());
                        }
                    }
                    //总原值 单价乘以数量
                    if (!isNullOrEmpty(assetFixedStorage.getAssetPrice())) {
                        BigDecimal totalOriginalPrice = new BigDecimal(assetFixedStorage.getAssetNum()).multiply(assetFixedStorage.getAssetPrice());
                        resp.setAssetTotalOriginalPrice(totalOriginalPrice);
                    }
                    resp.setAssetTotalNum(assetFixedStorage.getAssetNum());//总数量


                    //本月新增/减少
                    BigDecimal assetNewAddPrice = new BigDecimal("0.00");
                    BigDecimal assetReducePrice = new BigDecimal("0.00");

                    if (type == DATE_TYPE_MONTH) {//按月
                        // 设置年份
                        Calendar calendar = Calendar.getInstance();
                        calendar.set(Calendar.YEAR, Integer.parseInt(dateStr.substring(0,4)));
                        //设置月份
                        calendar.set(Calendar.MONTH, Integer.parseInt(dateStr.substring(5,7))-1);
                        calendar.set(Calendar.DATE, 1);
                        Date thisYearFistDate = calendar.getTime();
                        Date startDateOfMonth = TimeUtil.GetStartDateOfMonth(thisYearFistDate);//该月起始时间
                        Date endDateOfMonth = TimeUtil.GetEndDateOfMonth(thisYearFistDate);//该月最后一天

                        LambdaQueryWrapper<SmsAssetFixedStorage> assetFixedStorageQueryWrapper = new QueryWrapper<SmsAssetFixedStorage>()
                                .select("sum(asset_price) as assetNewAddPrice,sum(asset_per_depreciation_price) as assetReducePrice")
                                .lambda()
                                .eq(SmsAssetFixedStorage::getAssetName, assetFixedStorage.getAssetName())
                                .eq(SmsAssetFixedStorage::getAssetCategory, assetFixedStorage.getAssetCategory())
                                .eq(SmsAssetFixedStorage::getAssetPrice, assetFixedStorage.getAssetPrice())
                                .eq(SmsAssetFixedStorage::getAssetBrand, assetFixedStorage.getAssetBrand())
                                .eq(SmsAssetFixedStorage::getAssetModel, assetFixedStorage.getAssetModel())
                                .ge(SmsAssetFixedStorage::getCreateTime, startDateOfMonth)
                                .le(SmsAssetFixedStorage::getCreateTime, endDateOfMonth);
                        if (!isNullOrEmpty(assetFixedStorage.getAssetUnit())) {
                            assetFixedStorageQueryWrapper.eq(SmsAssetFixedStorage::getAssetUnit, assetFixedStorage.getAssetUnit());
                        }
                        List<Map<String, Object>> maps = smsAssetFixedStorageMapper.selectMaps(assetFixedStorageQueryWrapper);
                        if (maps != null && !maps.isEmpty()) {
                            for (Map<String, Object> map : maps) {
                                if (!isNullOrEmpty(map)) {
                                    SmsAssetFixedCategoryStatisticsResp addAndReducePriceResp = JSONObject.parseObject(JSON.toJSONString(map), SmsAssetFixedCategoryStatisticsResp.class);
                                    if (!isNullOrEmpty(addAndReducePriceResp)) {
                                        //新增
                                        assetNewAddPrice = isNullOrEmpty(addAndReducePriceResp.getAssetNewAddPrice()) ? new BigDecimal("0.00") : addAndReducePriceResp.getAssetNewAddPrice();
                                        //减少
                                        assetReducePrice = isNullOrEmpty(addAndReducePriceResp.getAssetReducePrice()) ? new BigDecimal("0.00") : addAndReducePriceResp.getAssetReducePrice();
                                    }
                                }
                            }
                        }
                    }else if (type == DATE_TYPE_YEAR) {//按年
                        LambdaQueryWrapper<SmsAssetFixedStorage> assetFixedStorageQueryWrapper = new LambdaQueryWrapper<SmsAssetFixedStorage>()
                                .eq(SmsAssetFixedStorage::getAssetName, assetFixedStorage.getAssetName())
                                .eq(SmsAssetFixedStorage::getAssetCategory, assetFixedStorage.getAssetCategory())
                                .eq(SmsAssetFixedStorage::getAssetPrice, assetFixedStorage.getAssetPrice())
                                .eq(SmsAssetFixedStorage::getAssetBrand, assetFixedStorage.getAssetBrand())
                                .eq(SmsAssetFixedStorage::getAssetModel, assetFixedStorage.getAssetModel())
                                .between(SmsAssetFixedStorage::getCreateTime, dateStr + "-01-01", dateStr + "-12-31");
                        if (!isNullOrEmpty(assetFixedStorage.getAssetUnit())) {
                            assetFixedStorageQueryWrapper.eq(SmsAssetFixedStorage::getAssetUnit, assetFixedStorage.getAssetUnit());
                        }
                        List<SmsAssetFixedStorage> fixedStorageList = smsAssetFixedStorageMapper.selectList(assetFixedStorageQueryWrapper);
                        if (fixedStorageList != null && !fixedStorageList.isEmpty()) {
                            for (SmsAssetFixedStorage smsAssetFixedStorage : fixedStorageList) {
                                assetNewAddPrice = assetNewAddPrice.add(smsAssetFixedStorage.getAssetPrice());
                                Date outStorageTime = smsAssetFixedStorage.getCreateTime();//入库时间
                                if (outStorageTime != null && outStorageTime.before(new Date()) && !isNullOrEmpty(smsAssetFixedStorage.getAssetTotalDepreciationPrice())) {
                                    // 获取当前日期
                                    String currentStr = TimeUtil.DateFormatSting(new Date());
                                    int currentYearStr = Integer.parseInt(currentStr.substring(0, 4));
                                    int currentMonthStr = Integer.parseInt(currentStr.substring(5, 7));

                                    // 获取给定日期的年份
                                    String outStorageTimeStr = TimeUtil.DateFormatSting(outStorageTime);
                                    int outStorageTimeYearStr = Integer.parseInt(outStorageTimeStr.substring(0, 4));
                                    int outStorageTimeMonthStr = Integer.parseInt(outStorageTimeStr.substring(5, 7));

                                    // 比较年份
                                    if (currentYearStr == outStorageTimeYearStr) {//本年
                                        assetReducePrice = assetReducePrice.add(smsAssetFixedStorage.getAssetTotalDepreciationPrice().multiply(new BigDecimal(currentMonthStr - outStorageTimeMonthStr)));
                                    }else {//不是本年
                                        assetReducePrice = assetReducePrice.add(smsAssetFixedStorage.getAssetTotalDepreciationPrice().multiply(new BigDecimal(currentMonthStr)));
                                    }
                                }
                            }
                        }
                    }

                    resp.setAssetNewAddPrice(assetNewAddPrice);
                    resp.setAssetReducePrice(assetReducePrice);
                    return resp;
                })
                .collect(Collectors.toList());
        return list;
    }
}
